package com.a2v2.online.store.database.dao.impl;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.a2v2.online.store.database.dao.contract.IUserSessionDAO;
import com.a2v2.online.store.database.entity.UserSession;

public class UserSessionDAOImpl extends GenericHibernateDAO<UserSession, Long>
		implements IUserSessionDAO {
	
	/**
	 * Retrieve the last logged in user.
	 */
	@Override
	public UserSession getLastLoggedInSession(Long userCredentialId) {
		Criteria criteria = this.getCurrrentSession().createCriteria(getPersistentClass());
		UserSession userSession = (UserSession) criteria.add(Restrictions.eq("userCredentialId", userCredentialId))
										.add(Restrictions.isNotNull("logOut"))										
										.addOrder(Order.desc("logOut"))
										.setMaxResults(1).uniqueResult();

		return userSession;		
	}

}
